package com.zds.support.util

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Build
import android.os.SystemClock
import android.text.InputType
import android.view.View
import android.widget.EditText

/**
 *
 *@Description
 *@Author zc
 *@Version V1.0
 **/
object SystemUtils {
    /**
     * 以下代码是为了解决华为安全键盘遮挡Toast问题
     */
    fun fkHuaweiSecurityKeyboard(context: Context) {
        try {
            if (checkIsHuaweiRom() && context is Activity) {
                val focusView: View = context.getWindow().getDecorView().findFocus()
                if (focusView is EditText) {
                    if (focusView.getInputType() == InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT) {
                        KeyBoardUtils.closeKeybord(context, focusView)
                    }
                }
            }
        } catch (e: Exception) {

        }

    }

    fun checkIsHuaweiRom(): Boolean {
        return Build.MANUFACTURER.contains("HUAWEI")
    }
    fun checkIsSamsung():Boolean{
        return Build.MANUFACTURER.contains("SAMSUNG",true)
    }
    /**
     * 复制内容到剪贴板
     */
    fun copyToClipboard(context: Context,
                        content: String) {
        try {
            val cm = context.getSystemService(Context.CLIPBOARD_SERVICE)
            if (cm is ClipboardManager) {
                val clip = ClipData.newPlainText(null, content)
                cm.setPrimaryClip(clip)
            }
        } catch (e: Exception) {

        }

    }
    private var recordServerTimeMillis = 0L

    private var recordElapsedRealtime = 0L
    @JvmStatic
    fun currentTimeMillis(): Long {
        if (recordServerTimeMillis > 0 && recordElapsedRealtime > 0) {

            val currentElapsedRealtime = SystemClock.elapsedRealtime()
            val elapsedRealtimeDiff = currentElapsedRealtime - recordElapsedRealtime
            val realTime = recordServerTimeMillis + elapsedRealtimeDiff


            /*Log.d("时间", String.format("记录服务器时间: %d, 记录本地机器时间：%d", recordServerTimeMillis,
                    recordElapsedRealtime))
            Log.d("时间", String.format("当前机器时间: %d, 时间差：%d ，实际时间：%d", currentElapsedRealtime,
                    elapsedRealtimeDiff, realTime))*/



            return realTime
        }
        /*if (TrueTime.isInitialized()) {
             return TrueTime.now().time
         } else {

         }*/
        return System.currentTimeMillis()
    }
}